package com.itheima.service.impl;

import com.itheima.bean.Student;
import com.itheima.mapper.CardMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import com.itheima.utils.MapperUtils;

import java.util.List;
import java.util.Map;

public class StudentServiceImpl implements StudentService {
    @Override
    public List<Student> findAll() {
        try{
            StudentMapper mapper = MapperUtils.getMapper(StudentMapper.class);
            List<Student> students = mapper.findAll();

            MapperUtils.commit();

            return students;
        }catch(Exception e){
            e.printStackTrace();
            MapperUtils.rollback();
        }finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public Integer save(Student stu) {
        try{
            StudentMapper mapper = MapperUtils.getMapper(StudentMapper.class);
            Integer count = mapper.save(stu);

            MapperUtils.commit();
            return count;
        }catch(Exception e){
            e.printStackTrace();
            MapperUtils.rollback();
        }finally {
            MapperUtils.close();
        }
        return null;
    }
    @Override
    public Integer update(Student stu) {
        try{
            StudentMapper mapper = MapperUtils.getMapper(StudentMapper.class);
            Integer count = mapper.update(stu);

            MapperUtils.commit();
            return count;
        }catch(Exception e){
            e.printStackTrace();
            MapperUtils.rollback();
        }finally {
            MapperUtils.close();
        }
        return null;
    }
    @Override
    public Integer delete(Integer id,String name) {
        try{
            StudentMapper mapper = MapperUtils.getMapper(StudentMapper.class);
            Integer count = mapper.delete(id,name);

            MapperUtils.commit();
            return count;
        }catch(Exception e){
            e.printStackTrace();
            MapperUtils.rollback();
        }finally {
            MapperUtils.close();
        }
        return null;
    }
}
